package com.crq.demo3;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;
public class CountGread {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        if (args.length != 2) {
            System.out.println("请提供输入路径和输出路径参数");
            System.exit(0);
        }

        // 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hadoop10:8020");
        // 2. 创建 job
        Job job = Job.getInstance(conf);
        job.setJarByClass(CountGread.class);

        // 3. 设置输入、输出格式化类

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        // 4. 设置输入、输出路径
//        TextInputFormat.addInputPath(job, new Path("/chengji.txt"));
//        TextOutputFormat.setOutputPath(job, new Path("/out4"));
        TextInputFormat.addInputPath(job, new Path(args[0]));
        TextOutputFormat.setOutputPath(job, new Path(args[1]));

        // 5. 设置 Mapper、Reducer
        job.setMapperClass(ScoreMapper.class);
        job.setReducerClass(ScoreReducer.class);

        // 6. 设置 Mapper、Reducer 的 KV 类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 7. 启动 job
        boolean result = job.waitForCompletion(true);
        System.out.println(result ? "成功" : "失败");
    }

    // Mapper：解析每行数据，输出 <姓名, 分数>
    static class ScoreMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            // 按空格分割（数据格式：序号 姓名 学科 分数 省份 ）
            String[] parts = line.split("\\s+");
            if (parts.length >= 4) { // 确保数据格式正确
                String name = parts[1];
                int score = Integer.parseInt(parts[3]);
                context.write(new Text(name), new IntWritable(score));
            }
        }
    }

    // Reducer：汇总每个学生的分数，计算总成绩
    static class ScoreReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int total = 0;
            for (IntWritable val : values) {
                total += val.get();
            }
            context.write(key, new IntWritable(total));
        }
    }
}

